package app.modules.common.util.auth;

import app.framework.constant.AuthConstants;
import cn.hutool.core.collection.CollectionUtil;
import jasmine.framework.common.util.StringUtil;
import jasmine.framework.context.CurrentSubject;
import jasmine.security.subject.UserSubject;

import java.util.Map;

/**
 * @author mh.z
 */
public class CurrentSubjectUtil {

    /**
     * 返回当前用户类型
     *
     * @return
     */
    public static String getUserType() {
        UserSubject subject = (UserSubject) CurrentSubject.getSubject();
        if (subject == null) {
            return null;
        }

        Map<String, Object> data = subject.getData();
        if (CollectionUtil.isEmpty(data)) {
            return null;
        }

        String userType = (String) data.get(AuthConstants.SUBJECT_DATA_USER_TYPE);

        return userType;
    }

    /**
     * 判断是否管理员用户
     *
     * @return
     */
    public static boolean isAdminUser() {
        String userType = getUserType();

        return (StringUtil.equals(userType, AuthConstants.USER_TYPE_SUPER_ADMIN)
                || StringUtil.equals(userType, AuthConstants.USER_TYPE_TENANT_ADMIN));
    }

    /**
     * 判断是否租户管理员
     *
     * @return
     */
    public static boolean isTenantAdmin() {
        String userType = getUserType();

        return (StringUtil.equals(userType, AuthConstants.USER_TYPE_TENANT_ADMIN));
    }

    /**
     * 判断是否系统管理员
     *
     * @return
     */
    public static boolean isSystemAdmin() {
        String userType = getUserType();

        return (StringUtil.equals(userType, AuthConstants.USER_TYPE_SUPER_ADMIN));
    }

}
